const api = require('../../utils/api.js');
const app = getApp();

let is_no_more = false;
let is_loading = false;
let p = 2;

Page({
  data: {
    order_list: [],
    show_no_data_tip: false,
    abnorData: {
      imgSrc: app.imgUrl + 'abnor-19.png',
      text: '暂无订单',
    },
    unit: '',
    type: 1, // 订单类型
  },

  onLoad() {
    let that = this;
    app.loadCurrencyUnit().then(res => {
      that.setData({
        unit: res.unit,
      });
    });
    app.pageOnLoad(this);
    is_no_more = false;
    is_loading = false;
    p = 2;
    that.loadOrderList();
  },

  onShow() {
    // if (this.data.type === 'pt') {
    //   this.getRobotList();
    // }
  },

  onReachBottom() {
    let that = this;
    if (is_loading || is_no_more) {
      return;
    }
    is_loading = true;
    wx.showLoading({
      title: '加载中...',
      mask: true,
    });
    app.ws_micro_shoprequest({
      url: api.user.clerk_info,
      data: {
        type: that.data.type,
        page: p,
      },
      success: function(res) {
        wx.hideLoading();
        if (res.code === 0) {
          let order_list = that.data.order_list.concat(res.data.list);
          that.setData({
            order_list: order_list,
          });
          if (res.data.list.length === 0) {
            is_no_more = true;
            // wx.showToast({
            //   title: '没有更多数据了',
            //   icon: 'none',
            // });
          }
        }
        p++;
      },
      complete: function() {
        is_loading = false;
      },
    });
  },

  loadOrderList() {
    let that = this;
    wx.showLoading({
      title: '正在加载',
      mask: true,
    });
    app.ws_micro_shoprequest({
      url: api.user.clerk_info,
      data: {
        type: that.data.type,
        page: 1,
      },
      success: function(res) {
        if (res.code === 0) {
          that.setData({
            order_list: res.data.list,
          });
          // console.info('------------order_list-----------', that.data.order_list)
        }
        that.setData({
          show_no_data_tip: that.data.order_list.length === 0,
        });
      },
      complete: function() {
        wx.hideLoading();
      },
    });
  },

  switchList(e) {
    wx.pageScrollTo({
      scrollTop: 0,
    });
    let data = e.currentTarget.dataset;
    this.setData({
      type: data.type,
    });
    is_no_more = false;
    is_loading = false;
    p = 2;
    this.loadOrderList();
  },

  getRobotList() {
    let that = this;
    app.ws_micro_shoprequest({
      url: api.adminer.robotList,
      success: function(res) {
        if (res.code === 0) {
          that.setData({
            robots: res.data,
          });
        }
      },
    });
  },
});
